
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>The sitemap framework &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="The “sites” framework" href="sites.html" />
    <link rel="prev" title="The redirects app" href="redirects.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="redirects.html" title="The redirects app">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="sites.html" title="The “sites” framework">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-sitemaps">
            
  <div class="section" id="s-module-django.contrib.sitemaps">
<span id="s-the-sitemap-framework"></span><span id="module-django.contrib.sitemaps"></span><span id="the-sitemap-framework"></span><h1>The sitemap framework<a class="headerlink" href="#module-django.contrib.sitemaps" title="Permalink to this headline">¶</a></h1>
<p>Django comes with a high-level sitemap-generating framework that makes
creating <a class="reference external" href="https://www.sitemaps.org/">sitemap</a> XML files easy.</p>
<div class="section" id="s-overview">
<span id="overview"></span><h2>Overview<a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h2>
<p>A sitemap is an XML file on your website that tells search-engine indexers how
frequently your pages change and how “important” certain pages are in relation
to other pages on your site. This information helps search engines index your
site.</p>
<p>The Django sitemap framework automates the creation of this XML file by letting
you express this information in Python code.</p>
<p>It works much like Django’s <a class="reference internal" href="syndication.html"><span class="doc">syndication framework</span></a>. To create a sitemap, just write a
<a class="reference internal" href="#django.contrib.sitemaps.Sitemap" title="django.contrib.sitemaps.Sitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sitemap</span></code></a> class and point to it in your
<a class="reference internal" href="../../topics/http/urls.html"><span class="doc">URLconf</span></a>.</p>
</div>
<div class="section" id="s-installation">
<span id="installation"></span><h2>Installation<a class="headerlink" href="#installation" title="Permalink to this headline">¶</a></h2>
<p>To install the sitemap app, follow these steps:</p>
<ol class="arabic simple">
<li>Add <code class="docutils literal notranslate"><span class="pre">'django.contrib.sitemaps'</span></code> to your <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a>
setting.</li>
<li>Make sure your <a class="reference internal" href="../settings.html#std:setting-TEMPLATES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TEMPLATES</span></code></a> setting contains a <code class="docutils literal notranslate"><span class="pre">DjangoTemplates</span></code>
backend whose <code class="docutils literal notranslate"><span class="pre">APP_DIRS</span></code> options is set to <code class="docutils literal notranslate"><span class="pre">True</span></code>. It’s in there by
default, so you’ll only need to change this if you’ve changed that setting.</li>
<li>Make sure you’ve installed the
<a class="reference internal" href="sites.html#module-django.contrib.sites" title="django.contrib.sites: Lets you operate multiple websites from the same database and Django project"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sites</span> <span class="pre">framework</span></code></a>.</li>
</ol>
<p>(Note: The sitemap application doesn’t install any database tables. The only
reason it needs to go into <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> is so that the
<a class="reference internal" href="../templates/api.html#django.template.loaders.app_directories.Loader" title="django.template.loaders.app_directories.Loader"><code class="xref py py-func docutils literal notranslate"><span class="pre">Loader()</span></code></a> template
loader can find the default templates.)</p>
</div>
<div class="section" id="s-initialization">
<span id="initialization"></span><h2>Initialization<a class="headerlink" href="#initialization" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="django.contrib.sitemaps.views.sitemap">
<code class="descclassname">views.</code><code class="descname">sitemap</code>(<em>request</em>, <em>sitemaps</em>, <em>section=None</em>, <em>template_name='sitemap.xml'</em>, <em>content_type='application/xml'</em>)<a class="headerlink" href="#django.contrib.sitemaps.views.sitemap" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>To activate sitemap generation on your Django site, add this line to your
<a class="reference internal" href="../../topics/http/urls.html"><span class="doc">URLconf</span></a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.sitemaps.views</span> <span class="k">import</span> <span class="n">sitemap</span>

<span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^sitemap\.xml$&#39;</span><span class="p">,</span> <span class="n">sitemap</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="n">sitemaps</span><span class="p">},</span>
    <span class="n">name</span><span class="o">=</span><span class="s1">&#39;django.contrib.sitemaps.views.sitemap&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>This tells Django to build a sitemap when a client accesses <code class="file docutils literal notranslate"><span class="pre">/sitemap.xml</span></code>.</p>
<p>The name of the sitemap file is not important, but the location is. Search
engines will only index links in your sitemap for the current URL level and
below. For instance, if <code class="file docutils literal notranslate"><span class="pre">sitemap.xml</span></code> lives in your root directory, it may
reference any URL in your site. However, if your sitemap lives at
<code class="file docutils literal notranslate"><span class="pre">/content/sitemap.xml</span></code>, it may only reference URLs that begin with
<code class="file docutils literal notranslate"><span class="pre">/content/</span></code>.</p>
<p>The sitemap view takes an extra, required argument: <code class="docutils literal notranslate"><span class="pre">{'sitemaps':</span> <span class="pre">sitemaps}</span></code>.
<code class="docutils literal notranslate"><span class="pre">sitemaps</span></code> should be a dictionary that maps a short section label (e.g.,
<code class="docutils literal notranslate"><span class="pre">blog</span></code> or <code class="docutils literal notranslate"><span class="pre">news</span></code>) to its <a class="reference internal" href="#django.contrib.sitemaps.Sitemap" title="django.contrib.sitemaps.Sitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sitemap</span></code></a> class
(e.g., <code class="docutils literal notranslate"><span class="pre">BlogSitemap</span></code> or <code class="docutils literal notranslate"><span class="pre">NewsSitemap</span></code>). It may also map to an <em>instance</em> of
a <a class="reference internal" href="#django.contrib.sitemaps.Sitemap" title="django.contrib.sitemaps.Sitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sitemap</span></code></a> class (e.g.,
<code class="docutils literal notranslate"><span class="pre">BlogSitemap(some_var)</span></code>).</p>
</div>
<div class="section" id="s-sitemap-classes">
<span id="sitemap-classes"></span><h2><code class="docutils literal notranslate"><span class="pre">Sitemap</span></code> classes<a class="headerlink" href="#sitemap-classes" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference internal" href="#django.contrib.sitemaps.Sitemap" title="django.contrib.sitemaps.Sitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sitemap</span></code></a> class is a simple Python
class that represents a “section” of entries in your sitemap. For example,
one <a class="reference internal" href="#django.contrib.sitemaps.Sitemap" title="django.contrib.sitemaps.Sitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sitemap</span></code></a> class could represent
all the entries of your Weblog, while another could represent all of the
events in your events calendar.</p>
<p>In the simplest case, all these sections get lumped together into one
<code class="file docutils literal notranslate"><span class="pre">sitemap.xml</span></code>, but it’s also possible to use the framework to generate a
sitemap index that references individual sitemap files, one per section. (See
<a class="reference internal" href="#creating-a-sitemap-index">Creating a sitemap index</a> below.)</p>
<p><a class="reference internal" href="#django.contrib.sitemaps.Sitemap" title="django.contrib.sitemaps.Sitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sitemap</span></code></a> classes must subclass
<code class="docutils literal notranslate"><span class="pre">django.contrib.sitemaps.Sitemap</span></code>. They can live anywhere in your codebase.</p>
</div>
<div class="section" id="s-a-simple-example">
<span id="a-simple-example"></span><h2>A simple example<a class="headerlink" href="#a-simple-example" title="Permalink to this headline">¶</a></h2>
<p>Let’s assume you have a blog system, with an <code class="docutils literal notranslate"><span class="pre">Entry</span></code> model, and you want your
sitemap to include all the links to your individual blog entries. Here’s how
your sitemap class might look:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.sitemaps</span> <span class="k">import</span> <span class="n">Sitemap</span>
<span class="kn">from</span> <span class="nn">blog.models</span> <span class="k">import</span> <span class="n">Entry</span>

<span class="k">class</span> <span class="nc">BlogSitemap</span><span class="p">(</span><span class="n">Sitemap</span><span class="p">):</span>
    <span class="n">changefreq</span> <span class="o">=</span> <span class="s2">&quot;never&quot;</span>
    <span class="n">priority</span> <span class="o">=</span> <span class="mf">0.5</span>

    <span class="k">def</span> <span class="nf">items</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Entry</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">is_draft</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">lastmod</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">pub_date</span>
</pre></div>
</div>
<p>Note:</p>
<ul class="simple">
<li><a class="reference internal" href="#django.contrib.sitemaps.Sitemap.changefreq" title="django.contrib.sitemaps.Sitemap.changefreq"><code class="xref py py-attr docutils literal notranslate"><span class="pre">changefreq</span></code></a> and <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.priority" title="django.contrib.sitemaps.Sitemap.priority"><code class="xref py py-attr docutils literal notranslate"><span class="pre">priority</span></code></a> are class
attributes corresponding to <code class="docutils literal notranslate"><span class="pre">&lt;changefreq&gt;</span></code> and <code class="docutils literal notranslate"><span class="pre">&lt;priority&gt;</span></code> elements,
respectively. They can be made callable as functions, as
<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.lastmod" title="django.contrib.sitemaps.Sitemap.lastmod"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lastmod</span></code></a> was in the example.</li>
<li><a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a> is simply a method that returns a list of
objects. The objects returned will get passed to any callable methods
corresponding to a sitemap property (<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.location" title="django.contrib.sitemaps.Sitemap.location"><code class="xref py py-attr docutils literal notranslate"><span class="pre">location</span></code></a>,
<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.lastmod" title="django.contrib.sitemaps.Sitemap.lastmod"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lastmod</span></code></a>, <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.changefreq" title="django.contrib.sitemaps.Sitemap.changefreq"><code class="xref py py-attr docutils literal notranslate"><span class="pre">changefreq</span></code></a>, and
<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.priority" title="django.contrib.sitemaps.Sitemap.priority"><code class="xref py py-attr docutils literal notranslate"><span class="pre">priority</span></code></a>).</li>
<li><a class="reference internal" href="#django.contrib.sitemaps.Sitemap.lastmod" title="django.contrib.sitemaps.Sitemap.lastmod"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lastmod</span></code></a> should return a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(in Python v3.7)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>.</li>
<li>There is no <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.location" title="django.contrib.sitemaps.Sitemap.location"><code class="xref py py-attr docutils literal notranslate"><span class="pre">location</span></code></a> method in this example, but you
can provide it in order to specify the URL for your object. By default,
<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.location" title="django.contrib.sitemaps.Sitemap.location"><code class="xref py py-attr docutils literal notranslate"><span class="pre">location()</span></code></a> calls <code class="docutils literal notranslate"><span class="pre">get_absolute_url()</span></code> on each object
and returns the result.</li>
</ul>
</div>
<div class="section" id="s-sitemap-class-reference">
<span id="sitemap-class-reference"></span><h2><code class="docutils literal notranslate"><span class="pre">Sitemap</span></code> class reference<a class="headerlink" href="#sitemap-class-reference" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.contrib.sitemaps.Sitemap">
<em class="property">class </em><code class="descname">Sitemap</code><a class="reference internal" href="../../_modules/django/contrib/sitemaps.html#Sitemap"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.sitemaps.Sitemap" title="Permalink to this definition">¶</a></dt>
<dd><p>A <code class="docutils literal notranslate"><span class="pre">Sitemap</span></code> class can define the following methods/attributes:</p>
<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.items">
<code class="descname">items</code><a class="reference internal" href="../../_modules/django/contrib/sitemaps.html#Sitemap.items"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.items" title="Permalink to this definition">¶</a></dt>
<dd><p><strong>Required.</strong> A method that returns a list of objects. The framework
doesn’t care what <em>type</em> of objects they are; all that matters is that
these objects get passed to the <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.location" title="django.contrib.sitemaps.Sitemap.location"><code class="xref py py-attr docutils literal notranslate"><span class="pre">location()</span></code></a>,
<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.lastmod" title="django.contrib.sitemaps.Sitemap.lastmod"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lastmod()</span></code></a>, <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.changefreq" title="django.contrib.sitemaps.Sitemap.changefreq"><code class="xref py py-attr docutils literal notranslate"><span class="pre">changefreq()</span></code></a> and
<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.priority" title="django.contrib.sitemaps.Sitemap.priority"><code class="xref py py-attr docutils literal notranslate"><span class="pre">priority()</span></code></a> methods.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.location">
<code class="descname">location</code><a class="reference internal" href="../../_modules/django/contrib/sitemaps.html#Sitemap.location"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.location" title="Permalink to this definition">¶</a></dt>
<dd><p><strong>Optional.</strong> Either a method or attribute.</p>
<p>If it’s a method, it should return the absolute path for a given object
as returned by <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a>.</p>
<p>If it’s an attribute, its value should be a string representing an
absolute path to use for <em>every</em> object returned by
<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a>.</p>
<p>In both cases, “absolute path” means a URL that doesn’t include the
protocol or domain. Examples:</p>
<ul class="simple">
<li>Good: <code class="file docutils literal notranslate"><span class="pre">'/foo/bar/'</span></code></li>
<li>Bad: <code class="file docutils literal notranslate"><span class="pre">'example.com/foo/bar/'</span></code></li>
<li>Bad: <code class="file docutils literal notranslate"><span class="pre">'https://example.com/foo/bar/'</span></code></li>
</ul>
<p>If <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.location" title="django.contrib.sitemaps.Sitemap.location"><code class="xref py py-attr docutils literal notranslate"><span class="pre">location</span></code></a> isn’t provided, the framework will call
the <code class="docutils literal notranslate"><span class="pre">get_absolute_url()</span></code> method on each object as returned by
<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a>.</p>
<p>To specify a protocol other than <code class="docutils literal notranslate"><span class="pre">'http'</span></code>, use
<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.protocol" title="django.contrib.sitemaps.Sitemap.protocol"><code class="xref py py-attr docutils literal notranslate"><span class="pre">protocol</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.lastmod">
<code class="descname">lastmod</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.lastmod" title="Permalink to this definition">¶</a></dt>
<dd><p><strong>Optional.</strong> Either a method or attribute.</p>
<p>If it’s a method, it should take one argument – an object as returned
by <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a> – and return that object’s last-modified
date/time as a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(in Python v3.7)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>.</p>
<p>If it’s an attribute, its value should be a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(in Python v3.7)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>
representing the last-modified date/time for <em>every</em> object returned by
<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a>.</p>
<p>If all items in a sitemap have a <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.lastmod" title="django.contrib.sitemaps.Sitemap.lastmod"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lastmod</span></code></a>, the sitemap
generated by <a class="reference internal" href="#django.contrib.sitemaps.views.sitemap" title="django.contrib.sitemaps.views.sitemap"><code class="xref py py-func docutils literal notranslate"><span class="pre">views.sitemap()</span></code></a> will have a <code class="docutils literal notranslate"><span class="pre">Last-Modified</span></code>
header equal to the latest <code class="docutils literal notranslate"><span class="pre">lastmod</span></code>. You can activate the
<a class="reference internal" href="../middleware.html#django.middleware.http.ConditionalGetMiddleware" title="django.middleware.http.ConditionalGetMiddleware"><code class="xref py py-class docutils literal notranslate"><span class="pre">ConditionalGetMiddleware</span></code></a> to make
Django respond appropriately to requests with an <code class="docutils literal notranslate"><span class="pre">If-Modified-Since</span></code>
header which will prevent sending the sitemap if it hasn’t changed.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.changefreq">
<code class="descname">changefreq</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.changefreq" title="Permalink to this definition">¶</a></dt>
<dd><p><strong>Optional.</strong> Either a method or attribute.</p>
<p>If it’s a method, it should take one argument – an object as returned
by <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a> – and return that object’s change
frequency as a string.</p>
<p>If it’s an attribute, its value should be a string representing the
change frequency of <em>every</em> object returned by <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a>.</p>
<p>Possible values for <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.changefreq" title="django.contrib.sitemaps.Sitemap.changefreq"><code class="xref py py-attr docutils literal notranslate"><span class="pre">changefreq</span></code></a>, whether you use a
method or attribute, are:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">'always'</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">'hourly'</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">'daily'</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">'weekly'</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">'monthly'</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">'yearly'</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">'never'</span></code></li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.priority">
<code class="descname">priority</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.priority" title="Permalink to this definition">¶</a></dt>
<dd><p><strong>Optional.</strong> Either a method or attribute.</p>
<p>If it’s a method, it should take one argument – an object as returned
by <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a> – and return that object’s priority as
either a string or float.</p>
<p>If it’s an attribute, its value should be either a string or float
representing the priority of <em>every</em> object returned by
<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a>.</p>
<p>Example values for <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.priority" title="django.contrib.sitemaps.Sitemap.priority"><code class="xref py py-attr docutils literal notranslate"><span class="pre">priority</span></code></a>: <code class="docutils literal notranslate"><span class="pre">0.4</span></code>, <code class="docutils literal notranslate"><span class="pre">1.0</span></code>. The
default priority of a page is <code class="docutils literal notranslate"><span class="pre">0.5</span></code>. See the <a class="reference external" href="https://www.sitemaps.org/protocol.html#prioritydef">sitemaps.org
documentation</a> for more.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.protocol">
<code class="descname">protocol</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.protocol" title="Permalink to this definition">¶</a></dt>
<dd><p><strong>Optional.</strong></p>
<p>This attribute defines the protocol (<code class="docutils literal notranslate"><span class="pre">'http'</span></code> or <code class="docutils literal notranslate"><span class="pre">'https'</span></code>) of the
URLs in the sitemap. If it isn’t set, the protocol with which the
sitemap was requested is used. If the sitemap is built outside the
context of a request, the default is <code class="docutils literal notranslate"><span class="pre">'http'</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.limit">
<code class="descname">limit</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.limit" title="Permalink to this definition">¶</a></dt>
<dd><p><strong>Optional.</strong></p>
<p>This attribute defines the maximum number of URLs included on each page
of the sitemap. Its value should not exceed the default value of
<code class="docutils literal notranslate"><span class="pre">50000</span></code>, which is the upper limit allowed in the <a class="reference external" href="https://www.sitemaps.org/protocol.html#index">Sitemaps protocol</a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.i18n">
<code class="descname">i18n</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.i18n" title="Permalink to this definition">¶</a></dt>
<dd><p><strong>Optional.</strong></p>
<p>A boolean attribute that defines if the URLs of this sitemap should
be generated using all of your <a class="reference internal" href="../settings.html#std:setting-LANGUAGES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LANGUAGES</span></code></a>. The default is
<code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-shortcuts">
<span id="shortcuts"></span><h2>Shortcuts<a class="headerlink" href="#shortcuts" title="Permalink to this headline">¶</a></h2>
<p>The sitemap framework provides a convenience class for a common case:</p>
<dl class="class">
<dt id="django.contrib.sitemaps.GenericSitemap">
<em class="property">class </em><code class="descname">GenericSitemap</code><a class="reference internal" href="../../_modules/django/contrib/sitemaps.html#GenericSitemap"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.sitemaps.GenericSitemap" title="Permalink to this definition">¶</a></dt>
<dd><p>The <a class="reference internal" href="#django.contrib.sitemaps.GenericSitemap" title="django.contrib.sitemaps.GenericSitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.contrib.sitemaps.GenericSitemap</span></code></a> class allows you to
create a sitemap by passing it a dictionary which has to contain at least
a <code class="docutils literal notranslate"><span class="pre">queryset</span></code> entry. This queryset will be used to generate the items
of the sitemap. It may also have a <code class="docutils literal notranslate"><span class="pre">date_field</span></code> entry that
specifies a date field for objects retrieved from the <code class="docutils literal notranslate"><span class="pre">queryset</span></code>.
This will be used for the <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.lastmod" title="django.contrib.sitemaps.Sitemap.lastmod"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lastmod</span></code></a> attribute in the
generated sitemap. You may also pass <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.priority" title="django.contrib.sitemaps.Sitemap.priority"><code class="xref py py-attr docutils literal notranslate"><span class="pre">priority</span></code></a> and
<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.changefreq" title="django.contrib.sitemaps.Sitemap.changefreq"><code class="xref py py-attr docutils literal notranslate"><span class="pre">changefreq</span></code></a> keyword arguments to the
<a class="reference internal" href="#django.contrib.sitemaps.GenericSitemap" title="django.contrib.sitemaps.GenericSitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">GenericSitemap</span></code></a>  constructor to specify
these attributes for all URLs.</p>
</dd></dl>

<div class="section" id="s-example">
<span id="example"></span><h3>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h3>
<p>Here’s an example of a <a class="reference internal" href="../../topics/http/urls.html"><span class="doc">URLconf</span></a> using
<a class="reference internal" href="#django.contrib.sitemaps.GenericSitemap" title="django.contrib.sitemaps.GenericSitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">GenericSitemap</span></code></a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">django.contrib.sitemaps</span> <span class="k">import</span> <span class="n">GenericSitemap</span>
<span class="kn">from</span> <span class="nn">django.contrib.sitemaps.views</span> <span class="k">import</span> <span class="n">sitemap</span>
<span class="kn">from</span> <span class="nn">blog.models</span> <span class="k">import</span> <span class="n">Entry</span>

<span class="n">info_dict</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;queryset&#39;</span><span class="p">:</span> <span class="n">Entry</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">(),</span>
    <span class="s1">&#39;date_field&#39;</span><span class="p">:</span> <span class="s1">&#39;pub_date&#39;</span><span class="p">,</span>
<span class="p">}</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="c1"># some generic view using info_dict</span>
    <span class="c1"># ...</span>

    <span class="c1"># the sitemap</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^sitemap\.xml$&#39;</span><span class="p">,</span> <span class="n">sitemap</span><span class="p">,</span>
        <span class="p">{</span><span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;blog&#39;</span><span class="p">:</span> <span class="n">GenericSitemap</span><span class="p">(</span><span class="n">info_dict</span><span class="p">,</span> <span class="n">priority</span><span class="o">=</span><span class="mf">0.6</span><span class="p">)}},</span>
        <span class="n">name</span><span class="o">=</span><span class="s1">&#39;django.contrib.sitemaps.views.sitemap&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-sitemap-for-static-views">
<span id="sitemap-for-static-views"></span><h2>Sitemap for static views<a class="headerlink" href="#sitemap-for-static-views" title="Permalink to this headline">¶</a></h2>
<p>Often you want the search engine crawlers to index views which are neither
object detail pages nor flatpages. The solution is to explicitly list URL
names for these views in <code class="docutils literal notranslate"><span class="pre">items</span></code> and call <a class="reference internal" href="../urlresolvers.html#django.urls.reverse" title="django.urls.reverse"><code class="xref py py-func docutils literal notranslate"><span class="pre">reverse()</span></code></a> in
the <code class="docutils literal notranslate"><span class="pre">location</span></code> method of the sitemap. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># sitemaps.py</span>
<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">sitemaps</span>
<span class="kn">from</span> <span class="nn">django.urls</span> <span class="k">import</span> <span class="n">reverse</span>

<span class="k">class</span> <span class="nc">StaticViewSitemap</span><span class="p">(</span><span class="n">sitemaps</span><span class="o">.</span><span class="n">Sitemap</span><span class="p">):</span>
    <span class="n">priority</span> <span class="o">=</span> <span class="mf">0.5</span>
    <span class="n">changefreq</span> <span class="o">=</span> <span class="s1">&#39;daily&#39;</span>

    <span class="k">def</span> <span class="nf">items</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span><span class="s1">&#39;main&#39;</span><span class="p">,</span> <span class="s1">&#39;about&#39;</span><span class="p">,</span> <span class="s1">&#39;license&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">location</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">reverse</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>

<span class="c1"># urls.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">django.contrib.sitemaps.views</span> <span class="k">import</span> <span class="n">sitemap</span>

<span class="kn">from</span> <span class="nn">.sitemaps</span> <span class="k">import</span> <span class="n">StaticViewSitemap</span>
<span class="kn">from</span> <span class="nn">.</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">sitemaps</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;static&#39;</span><span class="p">:</span> <span class="n">StaticViewSitemap</span><span class="p">,</span>
<span class="p">}</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">main</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;main&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^about/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">about</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;about&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^license/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">license</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;license&#39;</span><span class="p">),</span>
    <span class="c1"># ...</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^sitemap\.xml$&#39;</span><span class="p">,</span> <span class="n">sitemap</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="n">sitemaps</span><span class="p">},</span>
        <span class="n">name</span><span class="o">=</span><span class="s1">&#39;django.contrib.sitemaps.views.sitemap&#39;</span><span class="p">)</span>
<span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="s-creating-a-sitemap-index">
<span id="creating-a-sitemap-index"></span><h2>Creating a sitemap index<a class="headerlink" href="#creating-a-sitemap-index" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="django.contrib.sitemaps.views.index">
<code class="descclassname">views.</code><code class="descname">index</code>(<em>request</em>, <em>sitemaps</em>, <em>template_name='sitemap_index.xml'</em>, <em>content_type='application/xml'</em>, <em>sitemap_url_name='django.contrib.sitemaps.views.sitemap'</em>)<a class="headerlink" href="#django.contrib.sitemaps.views.index" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The sitemap framework also has the ability to create a sitemap index that
references individual sitemap files, one per each section defined in your
<code class="docutils literal notranslate"><span class="pre">sitemaps</span></code> dictionary. The only differences in usage are:</p>
<ul class="simple">
<li>You use two views in your URLconf: <a class="reference internal" href="#django.contrib.sitemaps.views.index" title="django.contrib.sitemaps.views.index"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.contrib.sitemaps.views.index()</span></code></a>
and <a class="reference internal" href="#django.contrib.sitemaps.views.sitemap" title="django.contrib.sitemaps.views.sitemap"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.contrib.sitemaps.views.sitemap()</span></code></a>.</li>
<li>The <a class="reference internal" href="#django.contrib.sitemaps.views.sitemap" title="django.contrib.sitemaps.views.sitemap"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.contrib.sitemaps.views.sitemap()</span></code></a> view should take a
<code class="docutils literal notranslate"><span class="pre">section</span></code> keyword argument.</li>
</ul>
<p>Here’s what the relevant URLconf lines would look like for the example above:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.sitemaps</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^sitemap\.xml$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">index</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="n">sitemaps</span><span class="p">}),</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^sitemap-(?P&lt;section&gt;.+)\.xml$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">sitemap</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="n">sitemaps</span><span class="p">},</span>
        <span class="n">name</span><span class="o">=</span><span class="s1">&#39;django.contrib.sitemaps.views.sitemap&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>This will automatically generate a <code class="file docutils literal notranslate"><span class="pre">sitemap.xml</span></code> file that references
both <code class="file docutils literal notranslate"><span class="pre">sitemap-flatpages.xml</span></code> and <code class="file docutils literal notranslate"><span class="pre">sitemap-blog.xml</span></code>. The
<a class="reference internal" href="#django.contrib.sitemaps.Sitemap" title="django.contrib.sitemaps.Sitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sitemap</span></code></a> classes and the <code class="docutils literal notranslate"><span class="pre">sitemaps</span></code>
dict don’t change at all.</p>
<p>You should create an index file if one of your sitemaps has more than 50,000
URLs. In this case, Django will automatically paginate the sitemap, and the
index will reflect that.</p>
<p>If you’re not using the vanilla sitemap view – for example, if it’s wrapped
with a caching decorator – you must name your sitemap view and pass
<code class="docutils literal notranslate"><span class="pre">sitemap_url_name</span></code> to the index view:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.sitemaps</span> <span class="k">import</span> <span class="n">views</span> <span class="k">as</span> <span class="n">sitemaps_views</span>
<span class="kn">from</span> <span class="nn">django.views.decorators.cache</span> <span class="k">import</span> <span class="n">cache_page</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^sitemap\.xml$&#39;</span><span class="p">,</span>
        <span class="n">cache_page</span><span class="p">(</span><span class="mi">86400</span><span class="p">)(</span><span class="n">sitemaps_views</span><span class="o">.</span><span class="n">index</span><span class="p">),</span>
        <span class="p">{</span><span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="n">sitemaps</span><span class="p">,</span> <span class="s1">&#39;sitemap_url_name&#39;</span><span class="p">:</span> <span class="s1">&#39;sitemaps&#39;</span><span class="p">}),</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^sitemap-(?P&lt;section&gt;.+)\.xml$&#39;</span><span class="p">,</span>
        <span class="n">cache_page</span><span class="p">(</span><span class="mi">86400</span><span class="p">)(</span><span class="n">sitemaps_views</span><span class="o">.</span><span class="n">sitemap</span><span class="p">),</span>
        <span class="p">{</span><span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="n">sitemaps</span><span class="p">},</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;sitemaps&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="s-template-customization">
<span id="template-customization"></span><h2>Template customization<a class="headerlink" href="#template-customization" title="Permalink to this headline">¶</a></h2>
<p>If you wish to use a different template for each sitemap or sitemap index
available on your site, you may specify it by passing a <code class="docutils literal notranslate"><span class="pre">template_name</span></code>
parameter to the <code class="docutils literal notranslate"><span class="pre">sitemap</span></code> and <code class="docutils literal notranslate"><span class="pre">index</span></code> views via the URLconf:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.sitemaps</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^custom-sitemap\.xml$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">index</span><span class="p">,</span> <span class="p">{</span>
        <span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="n">sitemaps</span><span class="p">,</span>
        <span class="s1">&#39;template_name&#39;</span><span class="p">:</span> <span class="s1">&#39;custom_sitemap.html&#39;</span>
    <span class="p">}),</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^custom-sitemap-(?P&lt;section&gt;.+)\.xml$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">sitemap</span><span class="p">,</span> <span class="p">{</span>
        <span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="n">sitemaps</span><span class="p">,</span>
        <span class="s1">&#39;template_name&#39;</span><span class="p">:</span> <span class="s1">&#39;custom_sitemap.html&#39;</span>
    <span class="p">},</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;django.contrib.sitemaps.views.sitemap&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>These views return <a class="reference internal" href="../template-response.html#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemplateResponse</span></code></a>
instances which allow you to easily customize the response data before
rendering. For more details, see the <a class="reference internal" href="../template-response.html"><span class="doc">TemplateResponse documentation</span></a>.</p>
<div class="section" id="s-context-variables">
<span id="context-variables"></span><h3>Context variables<a class="headerlink" href="#context-variables" title="Permalink to this headline">¶</a></h3>
<p>When customizing the templates for the
<a class="reference internal" href="#django.contrib.sitemaps.views.index" title="django.contrib.sitemaps.views.index"><code class="xref py py-func docutils literal notranslate"><span class="pre">index()</span></code></a> and
<a class="reference internal" href="#django.contrib.sitemaps.views.sitemap" title="django.contrib.sitemaps.views.sitemap"><code class="xref py py-func docutils literal notranslate"><span class="pre">sitemap()</span></code></a> views, you can rely on the
following context variables.</p>
</div>
<div class="section" id="s-index">
<span id="index"></span><h3>Index<a class="headerlink" href="#index" title="Permalink to this headline">¶</a></h3>
<p>The variable <code class="docutils literal notranslate"><span class="pre">sitemaps</span></code> is a list of absolute URLs to each of the sitemaps.</p>
</div>
<div class="section" id="s-id1">
<span id="id1"></span><h3>Sitemap<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
<p>The variable <code class="docutils literal notranslate"><span class="pre">urlset</span></code> is a list of URLs that should appear in the
sitemap. Each URL exposes attributes as defined in the
<a class="reference internal" href="#django.contrib.sitemaps.Sitemap" title="django.contrib.sitemaps.Sitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sitemap</span></code></a> class:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">changefreq</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">item</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">lastmod</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">location</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">priority</span></code></li>
</ul>
<p>The <code class="docutils literal notranslate"><span class="pre">item</span></code> attribute has been added for each URL to allow more flexible
customization of the templates, such as <a class="reference external" href="https://support.google.com/news/publisher/answer/74288?hl=en">Google news sitemaps</a>. Assuming
Sitemap’s <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a> would return a list of items with
<code class="docutils literal notranslate"><span class="pre">publication_data</span></code> and a <code class="docutils literal notranslate"><span class="pre">tags</span></code> field something like this would
generate a Google News compatible sitemap:</p>
<div class="highlight-xml+django notranslate"><div class="highlight"><pre><span></span><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="nt">&lt;urlset</span>
  <span class="na">xmlns=</span><span class="s">&quot;https://www.sitemaps.org/schemas/sitemap/0.9&quot;</span>
  <span class="na">xmlns:news=</span><span class="s">&quot;http://www.google.com/schemas/sitemap-news/0.9&quot;</span><span class="nt">&gt;</span>
<span class="cp">{%</span> <span class="k">spaceless</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">url</span> <span class="k">in</span> <span class="nv">urlset</span> <span class="cp">%}</span>
  <span class="nt">&lt;url&gt;</span>
    <span class="nt">&lt;loc&gt;</span><span class="cp">{{</span> <span class="nv">url.location</span> <span class="cp">}}</span><span class="nt">&lt;/loc&gt;</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">url.lastmod</span> <span class="cp">%}</span><span class="nt">&lt;lastmod&gt;</span><span class="cp">{{</span> <span class="nv">url.lastmod</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;Y-m-d&quot;</span> <span class="cp">}}</span><span class="nt">&lt;/lastmod&gt;</span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">url.changefreq</span> <span class="cp">%}</span><span class="nt">&lt;changefreq&gt;</span><span class="cp">{{</span> <span class="nv">url.changefreq</span> <span class="cp">}}</span><span class="nt">&lt;/changefreq&gt;</span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">url.priority</span> <span class="cp">%}</span><span class="nt">&lt;priority&gt;</span><span class="cp">{{</span> <span class="nv">url.priority</span> <span class="cp">}}</span><span class="nt">&lt;/priority&gt;</span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
    <span class="nt">&lt;news:news&gt;</span>
      <span class="cp">{%</span> <span class="k">if</span> <span class="nv">url.item.publication_date</span> <span class="cp">%}</span><span class="nt">&lt;news:publication_date&gt;</span><span class="cp">{{</span> <span class="nv">url.item.publication_date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;Y-m-d&quot;</span> <span class="cp">}}</span><span class="nt">&lt;/news:publication_date&gt;</span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
      <span class="cp">{%</span> <span class="k">if</span> <span class="nv">url.item.tags</span> <span class="cp">%}</span><span class="nt">&lt;news:keywords&gt;</span><span class="cp">{{</span> <span class="nv">url.item.tags</span> <span class="cp">}}</span><span class="nt">&lt;/news:keywords&gt;</span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
    <span class="nt">&lt;/news:news&gt;</span>
   <span class="nt">&lt;/url&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endspaceless</span> <span class="cp">%}</span>
<span class="nt">&lt;/urlset&gt;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-pinging-google">
<span id="pinging-google"></span><h2>Pinging Google<a class="headerlink" href="#pinging-google" title="Permalink to this headline">¶</a></h2>
<p>You may want to “ping” Google when your sitemap changes, to let it know to
reindex your site. The sitemaps framework provides a function to do just
that: <a class="reference internal" href="#django.contrib.sitemaps.ping_google" title="django.contrib.sitemaps.ping_google"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.contrib.sitemaps.ping_google()</span></code></a>.</p>
<dl class="function">
<dt id="django.contrib.sitemaps.ping_google">
<code class="descname">ping_google</code>()<a class="reference internal" href="../../_modules/django/contrib/sitemaps.html#ping_google"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.sitemaps.ping_google" title="Permalink to this definition">¶</a></dt>
<dd><p><a class="reference internal" href="#django.contrib.sitemaps.ping_google" title="django.contrib.sitemaps.ping_google"><code class="xref py py-func docutils literal notranslate"><span class="pre">ping_google()</span></code></a> takes an optional argument, <code class="docutils literal notranslate"><span class="pre">sitemap_url</span></code>,
which should be the absolute path to your site’s sitemap (e.g.,
<code class="file docutils literal notranslate"><span class="pre">'/sitemap.xml'</span></code>). If this argument isn’t provided,
<a class="reference internal" href="#django.contrib.sitemaps.ping_google" title="django.contrib.sitemaps.ping_google"><code class="xref py py-func docutils literal notranslate"><span class="pre">ping_google()</span></code></a> will attempt to figure out your
sitemap by performing a reverse looking in your URLconf.</p>
<p><a class="reference internal" href="#django.contrib.sitemaps.ping_google" title="django.contrib.sitemaps.ping_google"><code class="xref py py-func docutils literal notranslate"><span class="pre">ping_google()</span></code></a> raises the exception
<code class="docutils literal notranslate"><span class="pre">django.contrib.sitemaps.SitemapNotFound</span></code> if it cannot determine your
sitemap URL.</p>
</dd></dl>

<div class="admonition-register-with-google-first admonition">
<p class="first admonition-title">Register with Google first!</p>
<p class="last">The <a class="reference internal" href="#django.contrib.sitemaps.ping_google" title="django.contrib.sitemaps.ping_google"><code class="xref py py-func docutils literal notranslate"><span class="pre">ping_google()</span></code></a> command only works if you have registered your
site with <a class="reference external" href="https://www.google.com/webmasters/tools/">Google Webmaster Tools</a>.</p>
</div>
<p>One useful way to call <a class="reference internal" href="#django.contrib.sitemaps.ping_google" title="django.contrib.sitemaps.ping_google"><code class="xref py py-func docutils literal notranslate"><span class="pre">ping_google()</span></code></a> is from a model’s <code class="docutils literal notranslate"><span class="pre">save()</span></code>
method:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.sitemaps</span> <span class="k">import</span> <span class="n">ping_google</span>

<span class="k">class</span> <span class="nc">Entry</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="c1"># ...</span>
    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">force_insert</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">force_update</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Entry</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">force_insert</span><span class="p">,</span> <span class="n">force_update</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">ping_google</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
            <span class="c1"># Bare &#39;except&#39; because we could get a variety</span>
            <span class="c1"># of HTTP-related exceptions.</span>
            <span class="k">pass</span>
</pre></div>
</div>
<p>A more efficient solution, however, would be to call <a class="reference internal" href="#django.contrib.sitemaps.ping_google" title="django.contrib.sitemaps.ping_google"><code class="xref py py-func docutils literal notranslate"><span class="pre">ping_google()</span></code></a> from a
cron script, or some other scheduled task. The function makes an HTTP request
to Google’s servers, so you may not want to introduce that network overhead
each time you call <code class="docutils literal notranslate"><span class="pre">save()</span></code>.</p>
<div class="section" id="s-pinging-google-via-manage-py">
<span id="pinging-google-via-manage-py"></span><h3>Pinging Google via <code class="docutils literal notranslate"><span class="pre">manage.py</span></code><a class="headerlink" href="#pinging-google-via-manage-py" title="Permalink to this headline">¶</a></h3>
<dl class="django-admin">
<dt id="django-admin-ping_google">
<code class="descname">django-admin ping_google [sitemap_url]</code><a class="headerlink" href="#django-admin-ping_google" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Once the sitemaps application is added to your project, you may also
ping Google using the <code class="docutils literal notranslate"><span class="pre">ping_google</span></code> management command:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">manage</span><span class="o">.</span><span class="n">py</span> <span class="n">ping_google</span> <span class="p">[</span><span class="o">/</span><span class="n">sitemap</span><span class="o">.</span><span class="n">xml</span><span class="p">]</span>
</pre></div>
</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">The sitemap framework</a><ul>
<li><a class="reference internal" href="#overview">Overview</a></li>
<li><a class="reference internal" href="#installation">Installation</a></li>
<li><a class="reference internal" href="#initialization">Initialization</a></li>
<li><a class="reference internal" href="#sitemap-classes"><code class="docutils literal notranslate"><span class="pre">Sitemap</span></code> classes</a></li>
<li><a class="reference internal" href="#a-simple-example">A simple example</a></li>
<li><a class="reference internal" href="#sitemap-class-reference"><code class="docutils literal notranslate"><span class="pre">Sitemap</span></code> class reference</a></li>
<li><a class="reference internal" href="#shortcuts">Shortcuts</a><ul>
<li><a class="reference internal" href="#example">Example</a></li>
</ul>
</li>
<li><a class="reference internal" href="#sitemap-for-static-views">Sitemap for static views</a></li>
<li><a class="reference internal" href="#creating-a-sitemap-index">Creating a sitemap index</a></li>
<li><a class="reference internal" href="#template-customization">Template customization</a><ul>
<li><a class="reference internal" href="#context-variables">Context variables</a></li>
<li><a class="reference internal" href="#index">Index</a></li>
<li><a class="reference internal" href="#id1">Sitemap</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pinging-google">Pinging Google</a><ul>
<li><a class="reference internal" href="#pinging-google-via-manage-py">Pinging Google via <code class="docutils literal notranslate"><span class="pre">manage.py</span></code></a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="redirects.html"
                        title="previous chapter">The redirects app</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="sites.html"
                        title="next chapter">The “sites” framework</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/contrib/sitemaps.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="redirects.html" title="The redirects app">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="sites.html" title="The “sites” framework">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>